//
//  OMHomeSwipeTableCell.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/4/19.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMHomeSwipeTableCell.h"
#import "OMHomeSwipeGoodCollectCell.h"
#import "OMHomeSwipeCollectFooter.h"
#import "OMHomeCategory.h"
@interface OMHomeSwipeTableCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIImageView *iconBar;
@property (nonatomic, strong) UICollectionView *collectView;
@property (nonatomic, strong) OMHomeCategory *category;
@property (nonatomic, strong) UIImageView *moreIndicator;
@end

@implementation OMHomeSwipeTableCell

- (void)configure
{
    [super configure];
    self.contentView.backgroundColor = SM_backGroundColor;
    //icon
    _iconBar = [UIImageView new];
    _iconBar.image = [UIImage imageNamed:@"home_type_bar1"];
    [self.contentView addSubview:_iconBar];
    _iconBar.contentMode = UIViewContentModeScaleAspectFill;
    [_iconBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(@(SM_SIZE(20)));
        make.height.equalTo(@(SM_SCREEN_WIDTH/375.f*70));
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:nil action:nil];
    _iconBar.userInteractionEnabled = true;
    [_iconBar addGestureRecognizer:tap];
    @weakify(self);
    [[tap rac_gestureSignal]subscribeNext:^(id x) {
        @strongify(self);
        [self jumpToCategoryPage];
    }];

    //列表
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero
                                     collectionViewLayout:[self layout]];
    _collectView.delegate = self;
    _collectView.dataSource = self;
    _collectView.showsHorizontalScrollIndicator = NO;
    _collectView.backgroundColor = SM_whiteColor;
    [self.contentView insertSubview:_collectView belowSubview:_iconBar];
    [_collectView registerClass:[OMHomeSwipeGoodCollectCell class]
     forCellWithReuseIdentifier:[OMHomeSwipeGoodCollectCell cellIdentifier]];
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconBar.mas_bottom);
        make.bottom.right.left.equalTo(@0);
    }];

    //查看更多
    _moreIndicator = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"homeSwipe_footer"]];
    [self.moreIndicator sizeToFit];
    [self.collectView addSubview:_moreIndicator];

    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = SM_naviShadowColor;
    [self.contentView addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
        make.height.equalTo(@.5f);
    }];

}

- (void)configureCellWithObj:(OMHomeCategory *)obj
{
    if (obj == _category) {
        return;
    }
    self.category = obj;
    [_iconBar kt_imageWithURLStr:obj.image placeHolder:nil withSize:CGSizeZero];
    [self.collectView reloadData];
}

- (void)setCategory:(OMHomeCategory *)category
{
    _category = category;

    self.moreIndicator.center = CGPointMake(SM_SIZE(30)*2+itemSize*_category.children.count+SM_SIZE(20), itemSize/2);
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.moreIndicator.superview bringSubviewToFront:self.moreIndicator];
}

float itemSize = 0;
- (UICollectionViewLayout*)layout
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    itemSize = SM_SCREEN_WIDTH/2.5;
    flowLayout.itemSize = CGSizeMake(itemSize, itemSize*1.4);
    flowLayout.sectionInset = UIEdgeInsetsMake(SM_SIZE(10), SM_SIZE(30), SM_SIZE(25), SM_SIZE(30));
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    return flowLayout;
}

#pragma mark - collectionView delegate && datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.category.children.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OMHomeSwipeGoodCollectCell *cell = [OMHomeSwipeGoodCollectCell collectCellWithCollectionView:collectionView
                                         atIndexPath:indexPath];
    OMProduct *good = self.category.children[indexPath.item];
    [cell configureCellWithObj:good];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NaviPush(@"OMGoodDetailViewController", @{@"productId":[self.category.children[indexPath.item] id]});
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //查看更多，进入分类
    if (!scrollView.isDragging) {
        if(scrollView.contentOffset.x+scrollView.size.width > CGRectGetMaxX(self.moreIndicator.frame)){
            [self jumpToCategoryPage];
        }
    }
}

- (void)jumpToCategoryPage
{
    NaviPush(@"OMConsumerGoodsViewController", @{@"categoryBridgeId":self.category.id});
}

+ (CGFloat)cellHeight
{
    itemSize = SM_SCREEN_WIDTH/2.5;
    return SM_SCREEN_WIDTH/375.f*70+SM_SIZE(20)+itemSize*1.4+SM_SIZE(10)+SM_SIZE(25);
}




@end
